package com.burpee.web;

public class GeneratePaging {
	
	public String paging(int currentPage, 
			int totalCount,String url,int onepageCount, String viewPage, String controller) {

		int pageCount = totalCount / onepageCount;

		if (totalCount % onepageCount != 0) {
			pageCount++;
		}

		int currentPageBlock = currentPage / 10;

		int startPage = currentPageBlock * 10 + 1;
		int maxPage = pageCount;

		if (startPage + 10 < pageCount) {
			maxPage = startPage + 9;
		}

		

		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<a href=\"javascript:groupPaging('" + url + "&currentPage=1','" + viewPage + "','" +controller + "')\">" + "[처음]</a>");

		if (currentPageBlock < 1) {
			stringBuffer.append("[<<]");
		}
		else {
			stringBuffer.append("<a href=\"javascript:groupPaging('" + url + "&currentPage=" + (currentPageBlock * 10 - 9) + "','" + viewPage + "','" +controller + "')\">" + "[<<]</a>");
		}

		for (int i = startPage; i <= maxPage; i++) {

			if (i == currentPage) {
				stringBuffer.append("&nbsp;<b><a href=\"javascript:groupPaging('" + url + "&currentPage=" + i + "','" + viewPage + "','" +controller + "')\">" + i + "</a></b>&nbsp;");
			}
			else {
				stringBuffer.append("&nbsp;<a href=\"javascript:groupPaging('" + url + "&currentPage=" + i + "','" + viewPage + "','" +controller + "')\">" + i + "</a>&nbsp;");
			}

		}

		if (maxPage == pageCount) {
			stringBuffer.append("[>>]");
		}
		else {
			stringBuffer.append("<a href=\"javascript:groupPaging('" + url + "&currentPage=" + (currentPageBlock * 10 + 11) + "','" + viewPage + "','" +controller + "')\">" + "[>>]</a>");
		}
		stringBuffer.append("<a href=\"javascript:groupPaging('" + url + "&currentPage=" + pageCount + "','" + viewPage + "','" +controller + "')\">" + "[끝]</a>");

		return stringBuffer.toString();
	}
		
}
